import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    user: null,
    isAuthenticated: false
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user
      state.isAuthenticated = !!user
    },
    LOGOUT(state) {
      state.user = null
      state.isAuthenticated = false
    }
  },
  actions: {
    login({ commit }, userData) {
      // Simulate login API call
      return new Promise((resolve) => {
        setTimeout(() => {
          commit('SET_USER', userData)
          resolve(userData)
        }, 1000)
      })
    },
    logout({ commit }) {
      commit('LOGOUT')
    }
  },
  getters: {
    currentUser: state => state.user,
    isAuthenticated: state => state.isAuthenticated
  }
})
